package com.destinomusical.epura.parser;

import java.io.IOException;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;

import com.destinomusical.epura.extra.Errores;
import com.destinomusical.epura.obj.Cancion;
import com.destinomusical.epura.obj.Categoria;

public class ParserCanciones extends BaseFeedParser {
	final ArrayList<Cancion> lista = new ArrayList<Cancion>();
	static final String ROOT = "RESULTS";
	Cancion obj;
	int total;

	public ParserCanciones(String feedUrl) {
		super(feedUrl);
	}

	public ArrayList<Cancion> parse() {

		RootElement root = new RootElement(ROOT);

		root.getChild("TOTAL").setStartElementListener(
				new StartElementListener() {
					@Override
					public void start(Attributes attributes) {
						total = Integer.valueOf(attributes.getValue("rows"));
					}
				});

			
			Element row = root.getChild("ROW");
			row.setStartElementListener(new StartElementListener() {

				@Override
				public void start(Attributes arg0) {
					obj =  new Cancion();

				}
			});
			row.getChild("vid_idr").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("Categoria:" + body);
							obj.setId(Integer.parseInt(body));
						}
					});
			row.getChild("vid_nombre").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("imagen url:" + body);
							obj.setNombre(body);
						}
					});
			row.getChild("vid_url").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("imagen url:" + body);
							obj.setUrlId(body);
						}
					});
			row.getChild("vid_costo_desc").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("imagen url:" + body);
							obj.setCosto_desc(body);
						}
					});
			row.getChild("vid_costo").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("imagen url:" + body);
							obj.setCosto(body);
						}
					});
			row.getChild("vid_artista").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("imagen url:" + body);
							obj.setArtista(body);
						}
					});
			row.getChild("vid_codigo").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("imagen url:" + body);
							obj.setCodigo(body);
						}
					});
			row.getChild("vid_year").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("imagen url:" + body);
							obj.setYear(body);
						}
					});
			row.getChild("vid_portada").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("imagen url:" + body);
							obj.setComprado(body);
						}
					});
			row.getChild("total").setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("imagen url:" + body);
							obj.setTotal(body);
						}
					});

			row.setEndElementListener(new EndElementListener() {

				@Override
				public void end() {
					lista.add(obj);

				}
			});

		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
					root.getContentHandler());
		} catch (IOException e) {
			Errores.NO_CONNECTION = 1;
			Log.e("ParserCanciones", e.getMessage());
		} catch (SAXException e) {
			Log.e("ParserCanciones", e.getMessage());
		} catch (Exception e) {
			if (e.getLocalizedMessage() != null) {
				Log.e("ParserNegocio", e.getLocalizedMessage());
				Errores.NO_CONNECTION = 1;
			} else
				Log.e("ParserCanciones", "Unknown Exception");
		}
		return lista;
	}
}
